package com.envision.user.service.impl;

import com.envision.user.service.IUserService;
import com.envision.utils.AppConfig;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <strong>Title : UserServiceImpl.java<br>
 * </strong> <strong>Description : 用户service</strong><br>
 * <strong>Create on : 2018年03月07日<br>
 * <p>
 *
 * @author karrychang<br>
 * @version <strong>1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人		修改日期		修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("userServiceImpl")
public class UserServiceImpl implements IUserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private AppConfig appConfig;

    /** 
    *************************************************************************
     * UserServiceImpl.java——loginout<br>
     * Author: karrychang<br>
     * Date: 2018/7/16<br>
     * Description:<br>
     * Used in：<br>
     * @param request
     * @return
     *************************************************************************
     */
    public void loginout(HttpServletRequest request) {
        HttpSession session=request.getSession();
        session.invalidate();
        Cookie[] cookies=request.getCookies();
        if(cookies==null){
            return;
        }
        for (Cookie cookie:cookies){
            if("name".equals(cookie.getName())||"global_id".equals(cookie.getName())){
                cookie.setMaxAge(0);
            }
        }
    }

    /** 
    *************************************************************************
     * UserServiceImpl.java——login<br>
     * Author: karrychang<br>
     * Date: 2018/7/16<br>
     * Description:<br>
     * Used in：<br>
     * @param userName
     * @param password
     * @param response
     * @param request
     * @return
     *************************************************************************
     */
    public String login(String userName,String password,HttpServletResponse response,HttpServletRequest request){
        if ("admin".equals(userName)&&"1213".equals(password)){
            String str= UUID.randomUUID().toString().replaceAll("-","");
            String global_id="admin_"+str;
            Cookie cookie=new Cookie("global_id",global_id);
            response.addCookie(cookie);
            Cookie name=new Cookie("name",userName);
            response.addCookie(name);
            HttpSession session=request.getSession();
            session.setAttribute(userName,str);
            session.setMaxInactiveInterval(appConfig.getMaxInactiveInterval());
            Map<String , String> mssage=new HashMap<String , String>();
            mssage.put("global_id",global_id);
            mssage.put("name",userName);
            String msg ="{\"code\":200,\"msg\":\"success\",\"data\":"+ JSONObject.fromObject(mssage)+"}";
            return msg;
        }
        return "{\"code\":201,\"msg\":\"failed\",\"data\":null}";
    }
}
